<?php 
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\Db;
class Question extends Controller
{
	//åŠ è½½ç­”ç–‘é¡µé¢
	public function index($cid)
	{
		//æŸ¥è¯¢è¿™ä¸ªè¯¾æ‰€æœ‰çš„ç­”ç–‘é—®é¢˜ä»¥åŠå›žç­”
		$question = Db::name('question')
			->alias('q')
			->field('u.face,q.title,q.qcontent,q.qtime,q.status,q.rptime,q.rpname,q.rpcontent,u.nickname,u.id')
			->join('web_users u','q.uid=u.id')
			->order('q.qtime','desc')
			->select();
		// dump($question);die;
		$course = Db::name('myclass')->field('cname,cid')->where('uid',session('web')['uid'])->where('cid',$cid)->find();

		return view('my_course/question',['question'=>$question,'cid'=>$cid,'course'=>$course]); 
	}

	//åŠ è½½æˆ‘è¦æé—®é¡µé¢
	public function write_question($courseid)
	{
		$course = Db::name('myclass')->field('cname,cid')->where('uid',session('web')['uid'])->where('cid',$courseid)->find();
		return view('my_course/write_question',['course'=>$course,'cid'=>$courseid]);
	}

	//å¤„ç†æé—®çš„æäº?
	
	//处理提问的提交
	public function do_question($courseid)
	{
		// dump($courseid);die;
		$data = input('post.');
		// dump($data);die;
		$data['title'] = strip_tags($data['title']);
		$data['qcontent'] = strip_tags($data['qcontent']);

		$data['courseid'] = $courseid;
		$data['uid'] = session('web.uid');
		$data['qtime'] = time();
		// dump($data);die;
		$res = Db::name('question')->insert($data);
		if($res){
			$this->success('提交成功','/question/'.$courseid);
		}else{
			$this->error('提交失败,请重试');
		}
	}

	//答疑关键字搜索
	public function search_que()
	{
		$data= input('post.');
		$data['conlike'] = strip_tags($data['conlike']);
		// dump($data);die;
		$course = Db::name('myclass')->field('cname,cid')->where('uid',session('web')['uid'])->where('cid',$data['cid'])->find();
		
		$res = Db::name('question')
			->alias('q')
			->field('u.face,q.title,q.qcontent,q.qtime,q.status,q.rptime,q.rpname,q.rpcontent,u.nickname,u.id')
			->join('web_users u','q.uid=u.id')
			->where('title','like','%'.$data['conlike'].'%')
			->whereOr('qcontent','like','%'.$data['conlike'].'%')
			->select();
		// $res = Db::name('question')->where('title','like','%'.$data['conlike'].'%')->whereOr('qcontent','like','%'.$data['conlike'].'%')->select();
		// dump($res);die;
		return view('my_course/question',['question'=>$res,'course'=>$course,'cid'=>$data['cid'],'search'=>$data['conlike']]); 
	}


	//点击学生名字 跳转学生主页
	public function person($id)
	{
		$person = Db::name('web_users')->where('id',$id)->find();
		// dump($person);die;
		return view('my_course/person',['person'=>$person]);
	} 
	
}